package yt.kpool.view.pop;

import android.app.Activity;
import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.PopupWindow;

import java.util.List;

import yt.kpool.R;
import yt.kpool.adapter.MinerGroupSelectListviewAdapter;
import yt.kpool.adapter.MoveMinerGroupListviewAdapter;
import yt.kpool.bean.MinerGroupBean;
import yt.kpool.view.dialog.AddGroupDialog;

/**
 * 矿工分组pop
 * Created by Administrator on 2017/7/13.
 */

public class MoveMinerGroupPop extends PopupWindow {

    private Activity context;
    private View view;


    private OnSelectHouseLayoutListener onSelectHouseLayoutListener;
    private MoveMinerGroupListviewAdapter minerLayoutSelectListviewAdapter;

    private List<MinerGroupBean.DataBean> list;

    private String ChildAccountName;
    private String  CurrencyID;
    private String  ChildAccountID;
    public MoveMinerGroupPop(String ChildAccountName, String CurrencyID, String ChildAccountID, List<MinerGroupBean.DataBean> list, Context context, OnSelectHouseLayoutListener onSelectHouseLayoutListener) {
        super(context);
        this.context = (Activity) context;

        this.onSelectHouseLayoutListener = onSelectHouseLayoutListener;
        this.list = list;
        this.ChildAccountName = ChildAccountName;
        this.CurrencyID = CurrencyID;
        this.ChildAccountID = ChildAccountID;
        initPop();

        initView();

    }

    private void initView() {



        ListView lv_houselayout = view.findViewById(R.id.lv_houselayout);


        minerLayoutSelectListviewAdapter = new MoveMinerGroupListviewAdapter(context, CurrencyID, ChildAccountID, list, new MoveMinerGroupListviewAdapter.OnClickItemListener() {
            @Override
            public void clickItem() {
                dismiss();
            }
        });
        lv_houselayout.setAdapter(minerLayoutSelectListviewAdapter);

        lv_houselayout.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                onSelectHouseLayoutListener.selectHouseLayout(list.get(i).getName(),list.get(i).getID());


                dismiss();


            }
        });
    }


    /**
     * 初始化pop样式
     */
    private void initPop() {

        view = LayoutInflater.from(context).inflate(R.layout.pop_moveminergroup, null);
        this.setWidth(ViewGroup.LayoutParams.MATCH_PARENT);
        this.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
        this.setContentView(view);


        ColorDrawable dw = new ColorDrawable(context.getResources().getColor(R.color.gray_bg));
        //设置SelectPicPopupWindow弹出窗体的背景
        setBackgroundDrawable(dw);

        //获取popwindow焦点
        setFocusable(true);
        //设置popwindow如果点击外面区域，便关闭。
        setOutsideTouchable(true);
        //设置动画
//        setAnimationStyle(R.style.mypopwindow_anim_style);
        this.setOnDismissListener(new OnDismissListener() {
            @Override
            public void onDismiss() {
                backgroundAlpha(1f);
            }

        });
    }

    @Override
    public void showAtLocation(View parent, int gravity, int x, int y) {
        super.showAtLocation(parent, gravity, x, y);
        backgroundAlpha(0.3f);
    }


    /**
     * \
     * 设置透明背景
     *
     * @param bgAlpha
     */
    public void backgroundAlpha(float bgAlpha) {
        WindowManager.LayoutParams lp = context.getWindow().getAttributes();
        lp.alpha = bgAlpha; //0.0-1.0
        context.getWindow().setAttributes(lp);
    }

//    @Override
//    public void onClick(View view) {
//        switch (view.getId()) {
//
//            case R.id.view_null:
//                //点击半透明
//                dismiss();
//                break;
//            case R.id.ll_add:
//                dismiss();
//                new AddGroupDialog(context, ChildAccountName,0,"",CurrencyID,ChildAccountID, new AddGroupDialog.OnSuccessListener() {
//                    @Override
//                    public void getResult() {
//
//                    }
//                }).show();
//                break;
//
//
//        }
//    }

    /**
     * 矿工选择回调
     */
    public interface OnSelectHouseLayoutListener {
        public void selectHouseLayout(String name,int i);//不限



    }



}
